package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfoVo;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderDao orderDao;



    @Override
    public Result submit(OrderInfoVo orderInfoVo){
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        String idCard = orderInfoVo.getIdCard();
        String name = orderInfoVo.getName();
        Integer sex = orderInfoVo.getSex();
        Integer setmealId = orderInfoVo.getSetmealId();
        String orderType = orderInfoVo.getOrderType();

        //检查当前日期是否进行了预约设置
           OrderSetting orderSetting = orderSettingDao.findByDate(orderDate);
           if (orderSetting==null){
               return  Result.error("当前没有设置档期");
           }
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if (reservations>=number){
            return Result.error("当前人数预约已满");
        }
        //根页面用户输入的手机号码判断是否为会员
        Member member = memberDao.findByTelephone(telephone);
        //判断 如果不是就自动注册
        if (member==null){
             member=new Member();
            member.setPhoneNumber(telephone);
            member.setIdCard(idCard);
            member.setSex(String.valueOf(sex));
            member.setName(name);
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        //根据会员id 套餐id 预约时间查询是否重复预约
        Integer memberId = member.getId();
        Order queryOrder=new Order();
        queryOrder.setSetmealId(setmealId);
        queryOrder.setOrderDate(orderDate);
        queryOrder.setMemberId(memberId);
        List<Order> orders = orderDao.findByCondition(queryOrder);
           if (CollectionUtil.isNotEmpty(orders)){
               return  Result.error("不要重复预约");
           }
           //直接下单
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
         order.setSetmealId(setmealId);
        orderDao.add(order);
        //修改数据库中的预约人数 加1
       orderSetting.setReservations(orderSetting.getReservations()+1);
        int i = orderSettingDao.editReservations(orderSetting);
        if (i==0){
            //更新失败
            throw  new RuntimeException("更新失败");
        }
        return Result.success("",order);
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);

        return map;
    }
}
